package com.qf.realm;

import com.qf.bean.User;
import com.qf.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
 * projectName: 2103--RBAC
 *
 * @author: MaTianWei
 * time: 2021/7/9 16:21
 * description:
 */

public class AuthorizationRealm  extends AuthorizingRealm {
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        //1.获取用户的信息 账号  用户id
        //principals.getPrimaryPrincipal();获取的是 doGetAuthenticationInfo方法 返回的 AuthenticationInfo() 第一个参数! User
        User user = (User) principals.getPrimaryPrincipal();
        //2.查询用户对应的角色和权限
        InputStream stream = null;
        try {
            stream = Resources.getResourceAsStream("mybatis_config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(stream);

        SqlSession sqlSession = sessionFactory.openSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        List<String> roles = mapper.queryRoles(user.getuId());

        List<String> permissions = mapper.queryPermissions(user.getuId());


        //3.角色和权限装到AuthorizationInfo
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();

        authorizationInfo.addRoles(roles);
        authorizationInfo.addStringPermissions(permissions);


        return authorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {


        //1.获取token中的账号
        String account = (String) token.getPrincipal(); //输入的账号部分
        //2.拿账号去数据库查询
        InputStream stream = null;
        try {
            stream = Resources.getResourceAsStream("mybatis_config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }

        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(stream);

        SqlSession sqlSession = sessionFactory.openSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user = mapper.queryByAccount(account);

        //3.如果没有查到直接返回Null

        if (user == null) {

            return null;
        }

        //4.如果根据账号查询到了对应的用户 - 只需要返回密码即可

        SimpleAuthenticationInfo info =
                new SimpleAuthenticationInfo(user,user.getuPassword(), ByteSource.Util.bytes(user.getuSalt()),"myrealm");

        return info;
    }
}